﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Maticsoft.DBUtility;
using System.Data.SqlClient;
using V5_Model;

namespace V5_DAL
{
    public class DALCities
    {
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string area_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from K_Cities");
            strSql.Append(" where area_id=@area_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@area_id", SqlDbType.NVarChar,50)};
            parameters[0].Value = area_id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(ModelCities model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into K_Cities(");
            strSql.Append("area_id,area_name,area_level,area_parent_id)");
            strSql.Append(" values (");
            strSql.Append("@area_id,@area_name,@area_level,@area_parent_id)");
            SqlParameter[] parameters = {
					new SqlParameter("@area_id", SqlDbType.NVarChar,10),
					new SqlParameter("@area_name", SqlDbType.NVarChar,100),
					new SqlParameter("@area_level", SqlDbType.Int,4),
					new SqlParameter("@area_parent_id", SqlDbType.NVarChar,10)};
            parameters[0].Value = model.area_id;
            parameters[1].Value = model.area_name;
            parameters[2].Value = model.area_level;
            parameters[3].Value = model.area_parent_id;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(ModelCities model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update K_Cities set ");
            strSql.Append("area_name=@area_name,");
            strSql.Append("area_level=@area_level,");
            strSql.Append("area_parent_id=@area_parent_id");
            strSql.Append(" where area_id=@area_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@area_id", SqlDbType.NVarChar,10),
					new SqlParameter("@area_name", SqlDbType.NVarChar,100),
					new SqlParameter("@area_level", SqlDbType.Int,4),
					new SqlParameter("@area_parent_id", SqlDbType.NVarChar,10)};
            parameters[0].Value = model.area_id;
            parameters[1].Value = model.area_name;
            parameters[2].Value = model.area_level;
            parameters[3].Value = model.area_parent_id;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(string area_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from K_Cities ");
            strSql.Append(" where area_id=@area_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@area_id", SqlDbType.NVarChar,50)};
            parameters[0].Value = area_id;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModelCities GetModel(string area_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 area_id,area_name,area_level,area_parent_id from K_Cities ");
            strSql.Append(" where area_id=@area_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@area_id", SqlDbType.NVarChar,50)};
            parameters[0].Value = area_id;

            ModelCities model = new ModelCities();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.area_id = ds.Tables[0].Rows[0]["area_id"].ToString();
                model.area_name = ds.Tables[0].Rows[0]["area_name"].ToString();
                if (ds.Tables[0].Rows[0]["area_level"].ToString() != "")
                {
                    model.area_level = int.Parse(ds.Tables[0].Rows[0]["area_level"].ToString());
                }
                model.area_parent_id = ds.Tables[0].Rows[0]["area_parent_id"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select area_id,area_name,area_level,area_parent_id ");
            strSql.Append(" FROM K_Cities ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" area_id,area_name,area_level,area_parent_id ");
            strSql.Append(" FROM K_Cities ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "K_Cities";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }

        #endregion  成员方法
    }
}
